package com.zcq.jpush;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class Jdpush {
	  
    protected static final Logger LOG = LoggerFactory.getLogger(Jdpush.class);  
 
    // demo App defined in resources/jpush-api.conf   
 
   public static final String TITLE = "申通快递";  
   public static final String ALERT = "祝大家新春快乐";  
   public static final String MSG_CONTENT = "申通快递祝新老客户新春快乐";  
   public static final String REGISTRATION_ID = "0900e8d85ef";  
   public static final String TAG = "tag_api";  
     
   public  static JPushClient jpushClient=null;  
     
   public static void testSendPush(String appKey ,String masterSecret) {  
         
//        jpushClient = new JPushClient(masterSecret, appKey, 3);  
		jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
         
       // HttpProxy proxy = new HttpProxy("localhost", 3128);
       // Can use this https proxy: https://github.com/Exa-Networks/exaproxy  
        
         
       // For push, all you need do is to build PushPayload object.  
       //PushPayload payload = buildPushObject_all_all_alert();  
        //生成推送的内容，这里我们先测试全部推送  
//       PushPayload payload=buildPushObject_all_alias_alert();
       PushPayload payload=buildPushObject_android_tag_alertWithTitle();
         
         
       try {  
           System.out.println(payload.toString());  
           PushResult result = jpushClient.sendPush(payload);  
           System.out.println(result+"................................");  
             
           LOG.info("Got result - " + result);  
             
       } catch (APIConnectionException e) {  
           LOG.error("Connection error. Should retry later. ", e);  
             
       } catch (APIRequestException e) {  
           LOG.error("Error response from JPush server. Should review and fix it. ", e);  
           LOG.info("HTTP Status: " + e.getStatus());  
           LOG.info("Error Code: " + e.getErrorCode());  
           LOG.info("Error Message: " + e.getErrorMessage());  
           LOG.info("Msg ID: " + e.getMsgId());  
       }  
   }  
   /**
    * 
    * @Description: 所有平台 
    * @author: zhengchuanqing
    * @date: 2018年11月27日 下午11:00:24  
    * @param: 
    * @return: PushPayload
    * @throws
    */
   private static PushPayload buildPushObject_all_alias_alert() {  
       return PushPayload.newBuilder()  
               .setPlatform(Platform.all())//设置接受的平台  
               .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到  
               .setNotification(Notification.alert(ALERT))  
               .build();  
   }  
   /**
    * 
    * @Description: 推送android 
    * @author: zhengchuanqing
    * @date: 2018年11月27日 下午11:02:22  
    * @param: 
    * @return: PushPayload
    * @throws
    */
   private static PushPayload buildPushObject_android_tag_alertWithTitle() {  
       return PushPayload.newBuilder()  
               .setPlatform(Platform.android())  
               .setAudience(Audience.all())  
               .setNotification(Notification.android(ALERT, TITLE, null))  
               .build();  
   }  
   /**
    * 
    * @Description: 推送IOS 
    * @author: zhengchuanqing
    * @date: 2018年11月27日 下午11:03:04  
    * @param: 
    * @return: PushPayload
    * @throws
    */
   private static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {  
       return PushPayload.newBuilder()  
               .setPlatform(Platform.ios())  
               .setAudience(Audience.tag_and("tag1", "tag_all"))  
               .setNotification(Notification.newBuilder()  
                       .addPlatformNotification(IosNotification.newBuilder()  
                               .setAlert(ALERT)  
                               .setBadge(5)  
                               .setSound("happy")  
                               .addExtra("from", "JPush")  
                               .build())  
                       .build())  
                .setMessage(Message.content(MSG_CONTENT))  
                .setOptions(Options.newBuilder()  
                        .setApnsProduction(true)  
                        .build())  
                .build();  
   }  
}
